{% extends "BillingBundle::user_layout.html.twig" %}

{% block links %}
    <div class="wrapper">
        <div id="domain_add_tab" style="display:none">
            <a href="#domain_add" class="tab">Домены</a>
        </div>
        <table>
            <tr>
                <td>
                    <div id="shared_tab">
                        <a href="#shared" class="tab">Заказы</a>
                    </div>
                </td>
                <td>
                    <div id="domains_tab">
                        <a href="#domains" class="tab">Домены</a>
                    </div>
                </td>
                <td>
                    <div id="orders_show_tab">
                        <a href="#orders_show" class="tab">Заказать ещё</a>
                    </div>
                </td>
            </tr>
        </table>
    </div>
{% endblock %}

{% block content %}
    <div name="default" link="shared_tab" style="display:none">
        <a href="#shared">Заказы</a>
    </div>
    {#<div name="default" link="domains_tab" style="display:none">
        <a href="#domains">Домены</a>
    </div>#}
    <div name="default" link="orders_show_tab" style="display:none">
        <a href="#orders_show" class="btn btn-primary"><i class="icon-shopping-cart icon-white"></i> Заказать ещё</a>
    </div>
    <div name="default" link="messages_tab" style="display:none">
        <a href="#messages">Уведомления ({% render "BillingBundle:Messages:renderCount" %})</a> 
    </div>
    <div name="default" link="users_bills_add_tab" style="display:none">
        <a href="#users_bills_add" class="btn btn-success"><i class="icon-shopping-cart icon-white"></i> Пополнить счет</a>
    </div>
    <div name="default" link="users_bills_tab" style="display:none">
        <a href="#users_bills">Счета</a>
    </div>
    <div name="default" link="bonuses_tab" style="display:none">
        <a href="#bonuses">Бонусы</a>
    </div>
    {#<div name="default" link="personal_cabinet_tab" style="display:none">
        <a href="#personal_cabinet">Личный кабинет</a>
    </div>#}

    <link rel="stylesheet" type="text/css" media="screen" href="{{ asset('bundles/billing/css/validate.css') }}" />
    <script src="{{ asset('bundles/billing/js/jquery.min.js') }}" type="text/javascript"></script>
    <script src="{{ asset('bundles/billing/js/jquery.history.js') }}" type="text/javascript"></script>
    <script src="{{ asset('bundles/billing/js/jquery.validate.js') }}" type="text/javascript"></script>
    <script src="{{ asset('bundles/billing/js/jquery.timers.js') }}" type="text/javascript"></script>
    <script src="{{ asset('bundles/billing/js/code_decode.js') }}" type="text/javascript"></script>

    <script src="{{ asset('bundles/billing/js/shared_tabs.js') }}" type="text/javascript"></script>
    <script src="{{ asset('bundles/billing/js/bills_tabs.js') }}" type="text/javascript"></script>
    <script src="{{ asset('bundles/billing/js/validate_shared_email.js') }}" type="text/javascript"></script>
    <script src="{{ asset('bundles/billing/js/validate_shared_databases.js') }}" type="text/javascript"></script>
    <script src="{{ asset('bundles/billing/js/validate_shared_ftp.js') }}" type="text/javascript"></script>
    <script src="{{ asset('bundles/billing/js/validate_shared_dns.js') }}" type="text/javascript"></script>
    <script src="{{ asset('bundles/billing/js/validate_domain.js') }}" type="text/javascript"></script>
    <script type="text/javascript">

    function restoreLinks()
    {
        $('#shared_tab').html($('div[name="default"][link="shared_tab"]').html());
        //$('#domains_tab').html($('div[name="default"][link="domains_tab"]').html());
        $('#orders_show_tab').html($('div[name="default"][link="orders_show_tab"]').html());
        $('#messages_tab').html($('div[name="default"][link="messages_tab"]').html());
        $('#users_bills_add_tab').html($('div[name="default"][link="users_bills_add_tab"]').html());
        $('#users_bills_tab').html($('div[name="default"][link="users_bills_tab"]').html());
        $('#bonuses_tab').html($('div[name="default"][link="bonuses_tab"]').html());
        //$('#personal_cabinet_tab').html($('div[name="default"][link="personal_cabinet_tab"]').html());
        
        $('#shared_tab').removeClass("active");
        $('#domains_tab').removeClass("active");
        $('#orders_show_tab').removeClass("active");
        $('#messages_tab').removeClass("active");
        $('#users_bills_add_tab').removeClass("active");
        $('#users_bills_tab').removeClass("active");
        $('#bonuses_tab').removeClass("active");
        $('#personal_cabinet_tab').removeClass("active");
        return false;
    }
   
    function restore_email_validation(order, email)
    {
        var validator = $('#change_email_password_form[order="' + order + '"][emails="' + email + '"]').validate({
        rules: {
                change_email_password: {
                        required: true,
                        maxlength: 64,
                        minlength: 5,
                        check_password: true
                }
        },
        messages: {
                change_email_password: {
                        required: "Введите пароль",
                        maxlength: jQuery.format("Не более {0} символов"),
                        minlength: jQuery.format("Не менее {0} символов")
                }
        },

        errorPlacement: function(error, element) {
                error.appendTo( element.parent().next() );
        },
        success: function(label) {
                // set &nbsp; as text for IE
                label.html("&nbsp;").addClass("checked");
        }
    });
    }
    function restore_create_mysql_user_validation(order, base)
    {
        var validator = $('#create_mysql_user_form[order="' + order + '"][base="' + base + '"]').validate({
        rules: {
                user_mysql_login: {
                        check_mysql: true,
                        required: true,
                        maxlength: 9
                },
                user_mysql_password: {
                        required: true,
                        maxlength: 64,
                        minlength: 5,
                        check_password: true
                }
        },
        messages: {
                user_mysql_login: {
                        required: "Введите имя",
                        maxlength: jQuery.format("Не более {0} символов")
                },
                user_mysql_password: {
                        required: "Введите пароль",
                        maxlength: jQuery.format("Не более {0} символов"),
                        minlength: jQuery.format("Не менее {0} символов")
                }
        },

        errorPlacement: function(error, element) {
                error.appendTo( element.parent().next() );
        },
        success: function(label) {
                // set &nbsp; as text for IE
                label.html("&nbsp;").addClass("checked");
        }
    });
    }
    function restore_change_mysql_user_validation(order, base, user)
    {
        var validator = $('#change_mysql_user_form[order="' + order + '"][user="' + user + '"][base="' + base + '"]').validate({
        rules: {
                user_mysql_change_password: {
                        required: true,
                        maxlength: 64,
                        minlength: 5,
                        check_password: true
                }
        },
        messages: {
                user_mysql_change_password: {
                        required: "Введите пароль",
                        maxlength: jQuery.format("Не более {0} символов"),
                        minlength: jQuery.format("Не менее {0} символов")
                }
        },

        errorPlacement: function(error, element) {
                error.appendTo( element.parent().next() );
        },
        success: function(label) {
                // set &nbsp; as text for IE
                label.html("&nbsp;").addClass("checked");
        }
    });
    }
    function restore_change_ftp_validation(order, user)
    {
        var validator = $('#change_ftp_password_form[order="' + order + '"][user="' + user + '"]').validate({
        rules: {
                ftp_change_password: {
                        required: true,
                        maxlength: 64,
                        minlength: 5,
                        check_password: true
                }
        },
        messages: {
                ftp_change_password: {
                        required: "Введите пароль",
                        maxlength: jQuery.format("Не более {0} символов"),
                        minlength: jQuery.format("Не менее {0} символов")
                }
        },

        errorPlacement: function(error, element) {
                error.appendTo( element.parent().next() );
        },
        success: function(label) {
                // set &nbsp; as text for IE
                label.html("&nbsp;").addClass("checked");
        }
    });
    }
    function restore_change_dns_validation(order, hashs)
    {
        var validator = $('#dns_change_form[order="' + order + '"][hashs="' + hashs + '"]').validate({
        rules: {
                dns_change_name: {
                        required: true,
                        maxlength: 64,
                        check_dns: true
                },
                dns_change_value: {
                        required: true,
                        maxlength: 64,
                        check_dns: true
                }
        },
        messages: {
                dns_change_name: {
                        required: "Введите имя",
                        maxlength: jQuery.format("Не более {0} символов")
                },
                dns_change_value: {
                        required: "Введите значение",
                        maxlength: jQuery.format("Не более {0} символов")
                }
        },

        errorPlacement: function(error, element) {
                error.appendTo( element.parent().next() );
        },
        success: function(label) {
                // set &nbsp; as text for IE
                label.html("&nbsp;").addClass("checked");
        }
    });
    }
    function repeat_db_user_action(action, automatic)
    {
        pars = action.split(" ");
        var temp = pars[2].split("_");
        var order = temp[0];
        var user = temp[1];
        var temp2 = pars[3].split("_");
        var base = temp2[1];
        var item = $('#user_item[order="' + order + '"][user="' + user + '"][base="' + base + '"]');
        var img = $('#load_icon_wait_user[order="' + order + '"][user="' + user + '"][base="' + base + '"]');
        img.hide();
        if (pars[0] == 'create')
        {
            var password = pars[4];
            password = base64decode(password);
            item.remove();
            $('input[name="user_mysql_login"][order="' + order + '"][base="' + base + '"]').val(user);
            $('input[name="user_mysql_password"][order="' + order + '"][base="' + base + '"]').val(password);
            if (automatic)
                $('#add_mysql_user_content[order="' + order + '"][base="' + base + '"]').show(1000);
            else
                $('a#add_mysql_user[order="' + order + '"][base="' + base + '"]').trigger('click');
        }
        else if (pars[0] == 'drop')
        {
            $.ajax(
            {
                 type: 'POST',
                 data: {order:order, base:base, user:user},
                 url: "./shared_dbusers/active",
                 cache: false,
                 success: function(html)
                 {
                    item.replaceWith(html);
                    restore_change_mysql_user_validation(order, base, user);
                    $('a#drop_mysql_user[order="' + order + '"][user="' + user + '"][base="' + base + '"]').trigger('click');
                 }
            });     
        }
        else if (pars[0] == 'change')
        {
            if (pars[1] == 'mysql_user_password')
            {
                var password = pars[4];
                password = base64decode(password);
                $('input[name="user_mysql_change_password"][order="' + order + '"][user="' + user + '"][base="' + base + '"]').val(password);
                if (automatic)
                    $('#change_mysql_password_content[order="' + order + '"][base="' + base + '"]').show(1000);
                else
                    $('a#change_mysql_password[order="' + order + '"][user="' + user + '"][base="' + base + '"]').trigger('click');
            }
            else if (pars[1] == 'mysql_user_rights')
            {
                var rights = pars[4];
                //???
                $('a#change_mysql_rights[order="' + order + '"][user="' + user + '"][base="' + base + '"]').trigger('click');
            }
        }
    }
    function repeat_db_action(action, automatic)
    {
        pars = action.split(" ");
        var temp = pars[2].split("_");
        var order = temp[0];
        var base = temp[1];
        var item = $('#base_item[order="' + order + '"][base="' + base + '"]');
        var img = $('#load_icon_wait_db[order="' + order + '"][base="' + base + '"]');
        img.hide();
        if (pars[0] == 'create')
        {
            item.remove();
            $('input[name="database_name"][order="' + order + '"]').val(base);
            if (automatic)
                $('#add_database_content[order="' + order + '"]').show(1000);
            else
                $('a#add_database[order="' + order + '"]').trigger('click');
        }
        else if (pars[0] == 'drop')
        {
            $.ajax(
            {
                 type: 'POST',
                 data: {order:order, base:base},
                 url: "./shared_databases/active",
                 cache: false,
                 success: function(html)
                 {
                    item.replaceWith(html);
                    restore_create_mysql_user_validation(order, base);
                    $('a#drop_database[order="' + order + '"][base="' + base + '"]').trigger('click');
                 }
            });     
        }
    }
    function repeat_email_action(action, automatic)
    {
        pars = action.split(" ");
        var order = pars[2];
        var temp = pars[3].split("@");
        var user = temp[0];
        var domain = temp[1];
        var item = $('#email_item[order="' + order + '"][email="' + user + "@" + domain + '"]');
        var img = $('#load_icon_wait_email[order="' + order + '"][email="' + user + "@" + domain + '"]');
        img.hide();
        if (pars[0] == 'create')
        {
            item.remove();
            var password = pars[4];
            password = base64decode(password);
            $('input[name="email_user"][order="' + order + '"][domain="' + domain + '"]').val(user);
            $('input[name="email_password"][order="' + order + '"][domain="' + domain + '"]').val(password);
            //$('select[name="email_domains"][order="' + order + '"] option:selected').val(domain);
            if (automatic)
                $('#add_email_content[order="' + order + '"][domain="' + domain + '"]').show();
            else
                $('a#add_email[order="' + order + '"][domain="' + domain + '"]').trigger('click');
        }
        else if (pars[0] == 'drop')
        {
            $.ajax(
            {
                 type: 'POST',
                 data: {order:order, email:user + "@" + domain},
                 url: "./shared_email/active",
                 cache: false,
                 success: function(html)
                 {
                    item.replaceWith(html);
                    restore_email_validation(order, user + '@' + domain);
                    $('a#drop_email[order="' + order + '"][email="' + user + '@' + domain + '"]').trigger('click');
                 }
            });     
        }
        else if (pars[0] == 'change')
        {
            var password = pars[4]; 
            password = base64decode(password);
            $.ajax(
            {
                 type: 'POST',
                 data: {order:order, email:user + "@" + domain},
                 url: "./shared_email/active",
                 cache: false,
                 success: function(html)
                 {
                    item.replaceWith(html);
                    restore_email_validation(order, user + '@' + domain);
                    $('input[name="change_email_password"][order="' + order + '"][emails="' + user + '@' + domain + '"]').val(password);
                    if (automatic)
                        $('#change_email_password_content[order="' + order + '"][email="' + email + '"]').show();
                    else
                        $('a#change_email[order="' + order + '"][email="' + user + '@' + domain + '"]').trigger('click');
                 }
            });     
        }
    }
    function repeat_ftp_action(action, automatic)
    {
        pars = action.split(" ");
        var temp = pars[2].split("@");
        var user = temp[0];
        var order = temp[1];
        var item = $('#ftp_item[order="' + order + '"][user="' + user + '"]');
        var img = $('#load_icon_wait_ftp[order="' + order + '"][user="' + user + '"]');
        img.hide();
        if (pars[0] == 'create')
        {
            item.remove();
            var password = pars[3];
            password = base64decode(password);
            var dir = pars[4];
            $('input[name="ftp_user_name"][order="' + order + '"]').val(user);
            $('input[name="ftp_new_password"][order="' + order + '"]').val(password);
            $('input[name="ftp_directory"][order="' + order + '"]').val(dir);
            if (automatic)
                $('#add_ftp_content[order="' + order + '"]').show(); 
            else
                $('a#add_ftp[order="' + order + '"]').trigger('click');
        }
        else if (pars[0] == 'drop')
        {
            $.ajax(
            {
                 type: 'POST',
                 data: {order:order, user:user},
                 url: "./shared_ftp/active",
                 cache: false,
                 success: function(html)
                 {
                    item.replaceWith(html);
                    restore_change_ftp_validation(order, user);
                    $('a#drop_ftp[order="' + order + '"][user="' + user + '"]').trigger('click');
                 }
            });     
        }
        else if (pars[0] == 'change')
        {
            var password = pars[3]; 
            password = base64decode(password);
            $.ajax(
            {
                 type: 'POST',
                 data: {order:order, user:user},
                 url: "./shared_ftp/active",
                 cache: false,
                 success: function(html)
                 {
                    item.replaceWith(html);
                    restore_change_ftp_validation(order, user);
                    $('input[name="ftp_change_password"][order="' + order + '"][user="' + user + '"]').val(password);
                    if (automatic)
                        $('#change_ftp_password_content[order="' + order + '"][user="' + user + '"]').show();
                    else
                        $('a#change_ftp[order="' + order + '"][user="' + user + '"]').trigger('click');
                 }
            });     
        }
    }
    function repeat_dns_action(action, automatic)
    {
        pars = action.split(" ");
        var temp = pars[2].split("|");
        var order = temp[0];
        var domain = temp[1];
        var name = pars[3];
        var type = pars[4];
        var value = pars[5];
        var hashs = MD5(name + type + value + domain);
        var item = $('#dns_item[order="' + order + '"][hashs="' + hashs + '"]');
        var img = $('#load_icon_wait_dns[order="' + order + '"][hashs="' + hashs + '"]');
        img.hide();
        if (pars[0] == 'create')
        {
            item.remove();
            $('select[name="dns_type"][order="' + order + '"][domain="' + domain + '"] option:selected').val(type);
            $('input[name="dns_name"][order="' + order + '"][domain="' + domain + '"]').val(name);
            $('input[name="dns_value"][order="' + order + '"][domain="' + domain + '"]').val(value);
            if (!automatic)
                $('a#add_dns[order="' + order + '"][domain="' + domain + '"]').trigger('click');
        }
        else if (pars[0] == 'drop')
        {
            $.ajax(
            {
                 type: 'POST',
                 data: {order:order, hash:hashs},
                 url: "./shared_dns/active",
                 cache: false,
                 success: function(html)
                 {
                    item.remove();
                    item.append(html);
                    sort_dns(order);
                    //restore_change_ftp_validation(order, user);
                    $('a#drop_dns[order="' + order + '"][hashs="' + hashs + '"]').trigger('click');
                 }
            });     
        }
        else if (pars[0] == 'change')
        {
            var new_name = pars[6];
            var new_type = pars[7];
            var new_value = pars[8];
            $.ajax(
            {
                 type: 'POST',
                 data: {order:order, hash:hashs},
                 url: "./shared_dns/active",
                 cache: false,
                 success: function(html)
                 {
                    item.remove();
                    item.append(html);
                    sort_dns(order);
                    //restore_change_ftp_validation(order, user);
                    $('a#change_dns_link[order="' + order + '"][hashs="' + hashs + '"]').trigger('click');
                    $('input[name="dns_change_name"][order="' + order + '"][hashs="' + hashs + '"]').val(new_name);
                    $('input[name="dns_change_type"][order="' + order + '"][hashs="' + hashs + '"]').text(new_type);
                    $('input[name="dns_change_value"][order="' + order + '"][hashs="' + hashs + '"]').val(new_value);
                    if (!automatic)
                        $('a#change_dns[order="' + order + '"][hashs="' + hashs + '"]').trigger('click');
                 }
            });     
        }
    }

    $(document).ready(function()
    {
        //location.reload();
        /*var text = $('div.wrapper table tr td div a').html();
        $.ajax(
        {
             url: "./shared",
             cache: false,
             beforeSend: function()
             {
                $('#shared_tab').html('<b class="tab">' + text + '</b>');
                $('#content_tab').html('получение контента <img alt="загрузка" src="{{ asset('bundles/billing/i/load.gif') }}">');
             },
             success: function(html)
             {
                 $('#content_tab').html(html);
             }
        });*/
        $('#shared_tab > a').live('click', function()
        {
            var text = $(this).html();
            var url = $(this).attr('href');
            $.ajax(
            {
                 url: "./shared",
                 cache: false,
                 beforeSend: function()
                 {
                    restoreLinks();
                    //$('#shared_tab').html(text);
                    $('#shared_tab').addClass("active");
                    $('#content_tab').html('получение контента <img alt="загрузка" src="{{ asset('bundles/billing/i/load.gif') }}">');
                 },
                 success: function(html)
                 {
                    $('#content_tab').html(html);
                 }
            });
            return true;
        });
        $('#domains_tab > a').live('click', function()
        {
            var text = $(this).html();
            var url = $(this).attr('href');
            $.ajax(
            {
                 url: "./domains_show",
                 cache: false,
                 beforeSend: function()
                 {
                    restoreLinks();
                    $('#domains_tab').addClass("active");
                    //$('#domains_tab').html('<b>'+ text +'</b>');
                    $('#content_tab').html('получение контента <img alt="загрузка" src="{{ asset('bundles/billing/i/load.gif') }}">');
                 },
                 success: function(html)
                 {
                     $('#content_tab').html(html);
                 }
            });
            return true;
        });
        $('#orders_show_tab > a').live('click', function()
        {
            var text = $(this).html();
            var url = $(this).attr('href');
            $.ajax(
            {
                 url: "./orders_show",
                 cache: false,
                 beforeSend: function()
                 {
                    restoreLinks();
                    //$('#orders_show_tab').html(text);
                    $('#orders_show_tab').addClass("active");
                    $('#content_tab').html('получение контента <img alt="загрузка" src="{{ asset('bundles/billing/i/load.gif') }}">');
                 },
                 success: function(html)
                 {
                     $('#content_tab').html(html);
                 }
            });
            return true;
        });

        $('#users_bills_add_tab > a').live('click', function()
        {
            var text = $(this).html();
            $.ajax(
            {
                 url: "./bills_add",
                 cache: false,
                 beforeSend: function()
                 {
                    restoreLinks();
                    //$('#users_bills_add_tab').html(text);
                    $('#users_bills_add_tab').addClass("active");
                    $('#content_tab').html('получение контента <img alt="загрузка" src="{{ asset('bundles/billing/i/load.gif') }}">');
                 },
                 success: function(html)
                 {
                     $('#content_tab').html(html);
                 }
            });
            return true;
        });
        $('#users_bills_tab > a').live('click', function()
        {
            var text = $(this).html();
            $.ajax(
            {
                 url: "./bills_show",
                 cache: false,
                 beforeSend: function()
                 {
                    restoreLinks();
                    //$('#users_bills_tab').html(text);
                    $('#users_bills_tab').addClass("active");
                    $('#content_tab').html('получение контента <img alt="загрузка" src="{{ asset('bundles/billing/i/load.gif') }}">');
                 },
                 success: function(html)
                 {
                    $('#content_tab').html(html);
                 }
            });
            return true;
        });
        $('#bill').live('click', function()
        {
            var hash = $(this).attr("hash");
            $.ajax(
            {
                 url: "./bills_sources_show/" + hash,
                 cache: false,
                 beforeSend: function()
                 {
                    $('#content_tab').html('получение контента <img alt="загрузка" src="{{ asset('bundles/billing/i/load.gif') }}">');
                 },
                 success: function(html)
                 {
                    $('#content_tab').html(html);
                    $(this).everyTime(500, 'timer_bill', function()
                    {
                        var temp = location.hash.split(/:/);
                        if (temp[0] == "#users_bills" && temp[1])
                        {
                            restoreLinks();
                            $(this).stopTime('timer_bill');
                        }
                        if (temp[0] != "#users_bills")
                            $(this).stopTime('timer_bill');
                    });
                 }
            });
            return false;
        });
        $('div[name="bill_human_link"]').live('click', function()
        {
            var hash = $(this).attr("hash"); 
            var url = '#users_bills:bill=' + hash;
            location.href = 'panel' + url;
        });

        $('#bonuses_tab > a').live('click', function()
        {
            var text = $(this).html();
            $.ajax(
            {
                 url: "./bonuses_show",
                 cache: false,
                 beforeSend: function()
                 {
                    restoreLinks();
                    //$('#bonuses_tab').html(text);
                    $('#bonuses_tab').addClass("active");
                    $('#content_tab').html('получение контента <img alt="загрузка" src="{{ asset('bundles/billing/i/load.gif') }}">');
                 },
                 success: function(html)
                 {
                     $('#content_tab').html(html);
                 }
            });
            return true;
        });
        $('#personal_cabinet_tab > a').live('click', function()
        {
            var text = $(this).html();
            $.ajax(
            {
                 url: "./personal_cabinet",
                 cache: false,
                 beforeSend: function()
                 {
                    restoreLinks();
                    //$('#personal_cabinet_tab').html(text);
                    $('#personal_cabinet_tab').addClass("active");
                    $('#content_tab').html('получение контента <img alt="загрузка" src="{{ asset('bundles/billing/i/load.gif') }}">');
                 },
                 success: function(html)
                 {
                     $('#content_tab').html(html);
                 }
            });
            return true;
        });
        $('#messages_tab > a').live('click', function()
        {
            var text = $(this).html();
            $.ajax(
            {
                 url: "./messages_show",
                 cache: false,
                 beforeSend: function()
                 {
                    restoreLinks();
                    //$('#messages_tab').html(text);
                    $('#messages_tab').addClass("active");
                    $('#content_tab').html('получение контента <img alt="загрузка" src="{{ asset('bundles/billing/i/load.gif') }}">');
                 },
                 success: function(html)
                 {
                     $('#content_tab').html(html);
                 }
            });
            return true;
        });
        $('#message').live('click', function()
        {
            var message = $(this).attr("message");
            $.ajax(
            {
                 url: "./messages_show/" + message,
                 cache: false,
                 beforeSend: function()
                 {
                    $('#content_tab').html('получение контента <img alt="загрузка" src="{{ asset('bundles/billing/i/load.gif') }}">');
                 },
                 success: function(html)
                 {
                    $('#content_tab').html(html);
                    $(this).everyTime(500, 'timer_msg', function()
                    {
                        var temp = location.hash.split(/:/);
                        if (temp[0] == "#messages" && temp[1])
                        {
                            restoreLinks();
                            $(this).stopTime('timer_msg');
                        }
                        if (temp[0] != "#messages")
                            $(this).stopTime('timer_msg');
                    });
                 }
            });
            return false;
        });
        $('div[name="message_human_link"]').live('click', function()
        {
            var message = $(this).attr("message");
            var action = $(this).attr("action");    

            var url = '#messages:msg=' + message;
            location.href = 'panel' + url;
        });

        $('#domain_add_tab').live('click', function()
        {
            var text = $(this).html();
            var url = $(this).attr('href');
            $.ajax(
            {
                 url: "./domains_add",
                 beforeSend: function()
                 {
                    restoreLinks();
                    //$('#shared_tab').html(text);
                    $('#content_tab').html('получение контента <img alt="загрузка" src="{{ asset('bundles/billing/i/load.gif') }}">');
                 },
                 cache: false,
                 success: function(html)
                 {
                    $('#content_tab').html(html);
                 }
            });
            return true;
        });
        $('a#drop_domain').live('click', function()
        {
            var domain = $(this).attr("domain");
            $.ajax(
            {
                 type: 'POST',
                 data: {domain:domain},
                 url: "./domains_remove",
                 beforeSend: function()
                 {
                    $('#content_tab').html('получение контента <img alt="загрузка" src="{{ asset('bundles/billing/i/load.gif') }}">');
                 },
                 cache: false,
                 success: function(html)
                 {
                    $('#content_tab').html(html);
                 }
            });
            return false;
        });
        $('input[name="add_domain"]').live('click', function()
        {
            var order = $(this).attr("order");
            if (!$('#domain_form[order="' + order + '"]').valid())
                return false;
            var name = $('#domains_name').val();
            var ns1 = $('#domains_first_ns_server').val();
            var ns2 = $('#domains_second_ns_server').val();
            var file = $('select[name="template"] :selected').text();
            //$('#domain_form').submit();
            /*var domains = new Array();
            domains['name'] = $('#domains_name').val();
            domains['first_ns_server'] = $('#domains_first_ns_server').val();
            domains['second_ns_server'] = $('#domains_second_ns_server').val();
            domains['_token'] = $('#domains__token').val();*/
            if (order == '0')
                var url = "./domains_add";
            else
                var url = "./domains_add_to_order";
            $.ajax(
            {
                 type: 'POST',
                 data: {name:name, ns1:ns1, ns2:ns2, template:file},
                 url: url,
                 beforeSend: function()
                 {
                    restoreLinks();

                    $('#content_tab').html('получение контента <img alt="загрузка" src="{{ asset('bundles/billing/i/load.gif') }}">');
                 },
                 cache: false,
                 success: function(html)
                 {
                    $('#content_tab').html(html);
                 }
            });        
            return false;
        });
        $('#domains_name').live('click', function()
        {
            $(this).valid();
            return false;
        });
        $('select[name="template"]').live('change', function()
        {
            var file = $('select[name="template"] :selected').text();
            $.ajax(
            {
                 type: 'POST',
                 data: {file:file},
                 url: "./domains_show_template",
                 beforeSend: function()
                 {
                    $('#template_content').html('получение контента <img alt="загрузка" src="{{ asset('bundles/billing/i/load.gif') }}">');
                 },
                 cache: false,
                 success: function(html)
                 {
                    $('#template_content').html(html);
                 }
            });        
            return false;
        });
        $('#show_template').live('click', function()
        {
            $('#template_content').hide(1000);
            if ( $('#template_content').is(':hidden') )
            {
                $('select[name="template"]').trigger('change');
                $('#template_content').show(1000);
            }
            return false;
        });
        
        $('#repeate').live('click', function()
        {
            var action = $(this).attr('action');
            action = action.replace(/"/gi, '');
            var obj = $(this).attr('obj');
            if (obj == 'email')
                repeat_email_action(action, false);
            else if (obj == 'db_user')
                repeat_db_user_action(action, false);
            else if (obj == 'database')   
                repeat_db_action(action, false);
            else if (obj == 'ftp_user')   
                repeat_ftp_action(action, false);
            else if (obj == 'dns')   
                repeat_dns_action(action, false);
            return false;
        });
        $('#change').live('click', function()
        {
            var action = $(this).attr('action');
            action = action.replace(/"/gi, '');
            var obj = $(this).attr('obj');
            if (obj == 'email')
                repeat_email_action(action, true);
            else if (obj == 'db_user')
                repeat_db_user_action(action, true);
            else if (obj == 'database')   
                repeat_db_action(action, true);
            else if (obj == 'ftp_user')   
                repeat_ftp_action(action, true);
            else if (obj == 'dns')   
                repeat_dns_action(action, true);
            return false;
        });
        $('#hide').live('click', function()
        {
            var action = $(this).attr('action');
            action = action.replace(/"/gi, '');
            var obj = $(this).attr('obj');
            if (obj == 'db_user')
            {
                pars = action.split(" ");
                var temp = pars[2].split("_");
                var order = temp[0];
                var user = temp[1];
                var temp2 = pars[3].split("_");
                var base = temp2[1];
                var item = $('#user_item[order="' + order + '"][user="' + user + '"][base="' + base + '"]');
                var img = $('#load_icon_wait_user[order="' + order + '"][user="' + user + '"][base="' + base + '"]');
                img.hide();
                if (pars[0] == 'create')
                {
                    item.remove();
                }
                else
                {
                    $.ajax(
                    {
                         type: 'POST',
                         data: {order:order, base:base, user:user},
                         url: "./shared_dbusers/active",
                         cache: false,
                         success: function(html)
                         {
                            item.replaceWith(html);
                            restore_change_mysql_user_validation(order, base, user);
                         }
                    });
                }
            }
            else if (obj == 'database')   
            {
                pars = action.split(" ");
                var temp = pars[2].split("_");
                var order = temp[0];
                var base = temp[1];
                var item = $('#base_item[order="' + order + '"][base="' + base + '"]');
                var img = $('#load_icon_wait_db[order="' + order + '"][base="' + base + '"]');
                img.hide();
                if (pars[0] == 'create')
                {
                    item.remove();
                }
                else
                {
                    $.ajax(
                    {
                         type: 'POST',
                         data: {order:order, base:base},
                         url: "./shared_databases/active",
                         cache: false,
                         success: function(html)
                         {
                            item.replaceWith(html);
                            restore_create_mysql_user_validation(order, base);
                         }
                    });
                }
            }
            else if (obj == 'email')
            {
                pars = action.split(" ");
                var order = pars[2];
                var temp = pars[3].split("@");
                var user = temp[0];
                var domain = temp[1];
                var item = $('#email_item[order="' + order + '"][email="' + user + "@" + domain + '"]');
                var img = $('#load_icon_wait_email[order="' + order + '"][email="' + user + "@" + domain + '"]');
                img.hide();
                if (pars[0] == 'create')
                {
                    item.remove();
                }
                else
                {
                    $.ajax(
                    {
                         type: 'POST',
                         data: {order:order, email:user + "@" + domain},
                         url: "./shared_email/active",
                         cache: false,
                         success: function(html)
                         {
                            item.replaceWith(html);
                            restore_email_validation(order, user + '@' + domain);
                         }
                    });
                }
            }
            else if (obj == 'ftp_user')
            {
                pars = action.split(" ");
                var temp = pars[2].split("@");
                var user = temp[0];
                var order = temp[1];
                var item = $('#ftp_item[order="' + order + '"][user="' + user + '"]');
                var img = $('#load_icon_wait_ftp[order="' + order + '"][user="' + user + '"]');
                img.hide();
                if (pars[0] == 'create')
                {
                    item.remove();
                }
                else
                {
                    $.ajax(
                    {
                         type: 'POST',
                         data: {order:order, user:user},
                         url: "./shared_ftp/active",
                         cache: false,
                         success: function(html)
                         {
                            item.replaceWith(html);
                            restore_change_ftp_validation(order, user);
                         }
                    });
                }
            }
            else if (obj == 'dns')
            {
                pars = action.split(" ");
                var temp = pars[2].split("|");
                var order = temp[0];
                var domain = temp[1];
                var name = pars[3];
                var type = pars[4];
                var value = pars[5];
                var hashs = MD5(name + type + value + domain);
                var item = $('#dns_item[order="' + order + '"][hashs="' + hashs + '"]');
                var img = $('#load_icon_wait_dns[order="' + order + '"][hashs="' + hashs + '"]');
                img.hide();
                if (pars[0] == 'create')
                {
                    item.remove();
                }
                else
                {
                    $.ajax(
                    {
                         type: 'POST',
                         data: {order:order, hash:hashs},
                         url: "./shared_dns/active",
                         cache: false,
                         success: function(html)
                         {
                            item.remove();
                            item.append(html);
                            sort_dns(order);
                            //restore_email_validation(order, user + '@' + domain);
                         }
                    });
                }
            }
            return false;
        });
    });

    </script>
    <script src="{{ asset('bundles/billing/js/ajax.js') }}" type="text/javascript"></script>
    <tr>
	<td class="content">
            <div id="content_tab"></div>
	</td>
    </tr>
{% endblock %}
